package com.zhss.demo.zookeeper;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;

public class CheckedStream {
    public static void runCheckedInputStream(String fileName) throws IOException {
        FileInputStream in = new FileInputStream(fileName);
        CheckedInputStream checked = new CheckedInputStream(in, new Adler32());
        byte[] b = new byte[4096];
        while ((checked.read(b)) != -1) {
        }
        in.close();
        checked.close();
        System.out.println(fileName+" CheckedInputStream Checksum: " + checked.getChecksum().getValue());
    }

    public static void runCheckedOutputStream(String fileName) throws IOException {
        FileOutputStream output = new FileOutputStream(fileName);
        CheckedOutputStream checked = new CheckedOutputStream(output,
                new Adler32());
        String str = "Hello, world!";
        checked.write(str.getBytes(), 0, str.length());
        System.out.println(fileName+" CheckedOutputStream Checksum: " + checked.getChecksum().getValue());
    }
    public static void main(String[] args) throws IOException {
        runCheckedInputStream("/Users/stone/Desktop/60593FEB-EED6-459F-A68B-D91B9A02F7EA.pdf");
        runCheckedInputStream("/Users/stone/Desktop/60593FEB-EED6-459F-A68B-D91B9A02F7EA 2.pdf");
        runCheckedOutputStream("/Users/stone/Desktop/IOTest1.text");
        runCheckedOutputStream("/Users/stone/Desktop/IOTest2.text");
        runCheckedInputStream("/Users/stone/Desktop/IOTest1.text");
        runCheckedInputStream("/Users/stone/Desktop/IOTest2.text");
    }
}
